package com.sugar.queryinfo.module.business.controller.admin.userextend;

import com.sugar.queryinfo.framework.idempotent.core.annotation.Idempotent;
import com.sugar.queryinfo.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import com.sugar.queryinfo.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.math.BigDecimal;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.controller.admin.userextend.vo.*;
import com.sugar.queryinfo.module.business.dal.dataobject.userextend.UserExtendDO;
import com.sugar.queryinfo.module.business.service.userextend.UserExtendService;

@Tag(name = "管理后台 - 用户信息拓展")
@RestController
@RequestMapping("/business/user-extend")
@Validated
public class UserExtendController {

    @Resource
    private UserExtendService userExtendService;

    @PostMapping("/update/balance")
    @Operation(summary = "分配余额")
    @PreAuthorize("@ss.hasPermission('business:user-extend:update')")
    public CommonResult<Boolean> updateUserBalance(@RequestBody @Valid UserExtendSaveReqVO reqVO) {
        userExtendService.updateUserBalance(reqVO);
        return success(true);
    }

    @GetMapping("/refresh/balance")
    @Operation(summary = "刷新余额")
    @Idempotent(message = "您的手速太快了！", keyResolver = UserIdempotentKeyResolver.class)
    public CommonResult<BigDecimal> refreshUserBalance() {
        return success(userExtendService.refreshUserBalance(SecurityFrameworkUtils.getLoginUserId()));
    }

    @GetMapping("/get/qrcode")
    @Operation(summary = "获取用户的二维码")
    public CommonResult<String> getUserQrCode() {
        return success(userExtendService.getUserQrCode(SecurityFrameworkUtils.getLoginUserId()));
    }

    @GetMapping("/refresh/qrcode")
    @Operation(summary = "刷新用户的二维码")
    public CommonResult<String> refreshUserQrCode() {
        return success(userExtendService.refreshUserQrCode(SecurityFrameworkUtils.getLoginUserId()));
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息拓展")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:user-extend:query')")
    public CommonResult<UserExtendRespVO> getUserExtend(@RequestParam("id") Long id) {
        UserExtendDO userExtend = userExtendService.getUserExtend(id);
        return success(BeanUtils.toBean(userExtend, UserExtendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息拓展分页")
    @PreAuthorize("@ss.hasPermission('business:user-extend:query')")
    public CommonResult<PageResult<UserExtendRespVO>> getUserExtendPage(@Valid UserExtendPageReqVO pageReqVO) {
        return success(userExtendService.getUserExtendPage(pageReqVO));
    }
}